package cn.com.haopy.dataes.db.service.main.impl;

import cn.com.haopy.dataes.db.entity.main.HDStorage;
import cn.com.haopy.dataes.db.mapper.main.HDStorageMapper;
import cn.com.haopy.dataes.db.service.main.HDStorageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class HDStorageServiceImpl extends ServiceImpl<HDStorageMapper, HDStorage> implements HDStorageService {
    @Override
    public void deleteByKey(String key) {
        LambdaQueryWrapper<HDStorage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDStorage::getKey, key);

        super.remove(queryWrapper);
    }

    @Override
    public HDStorage findByKey(String key) {
        LambdaQueryWrapper<HDStorage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDStorage::getKey, key);

        return super.getOne(queryWrapper);
    }

    @Override
    public Page<HDStorage> querySelective(String key, String name, Integer pageNo, Integer size, String sort, String order) {
        QueryWrapper<HDStorage> queryWrapper = new QueryWrapper<>();

        if (StringUtils.hasText(key)) {
            queryWrapper.lambda().eq(HDStorage::getKey, key);
        }
        if (StringUtils.hasText(name)) {
            queryWrapper.lambda().like(HDStorage::getName, name);
        }

        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order),sort);
        }
        Page<HDStorage> page = new Page<>(pageNo, size);

        return super.page(page, queryWrapper);
    }
}
